package com.usian.controller;

import com.usian.common.Result;
import com.usian.pojo.City;
import com.usian.pojo.User;
import com.usian.service.CityService;
import com.usian.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


@RestController
@RequestMapping("/city")
@CrossOrigin
public class CityController {
    private final CityService cityService;

    @Autowired
    private IUserService userService;

    public CityController(CityService cityService) {
        this.cityService = cityService;
    }

    @RequestMapping("/list")
    public Result<City> listAllCities() {
        return cityService.findList();
    }

    @PostMapping("/add")
    public Result addCity(@RequestBody City city) {
        return cityService.add(city);
    }

    @RequestMapping("/update")
    public ResponseEntity<Boolean> updateCity(@RequestBody City city) {
        boolean result = cityService.updateCity(city);
        return new ResponseEntity<>(result, result? HttpStatus.OK : HttpStatus.INTERNAL_SERVER_ERROR);
    }

    @GetMapping("/getById/{id}")
    public Result<City> getCityById(@PathVariable Integer id) {
        City city = cityService.getById(id);
        return city != null ? Result.ok(city) : Result.fail();
    }


    @PostMapping("/pause/{id}")
    public Result pauseCity(@PathVariable Integer id) {
        City city = new City();
        city.setId(id);
        city.setStatus("暂停服务");
        city.setUpdateTime(new Date());
        return cityService.updateById(city) ? Result.ok() : Result.fail();
    }
    @PostMapping("/start/{id}")
    public Result startCity(@PathVariable Integer id) {
        City city = new City();
        city.setId(id);
        city.setStatus("服务中");
        city.setUpdateTime(new Date());
        return cityService.updateById(city) ? Result.ok() : Result.fail();
    }
}